<template>
  <div class="sub-sort ">
    <div class="sort">
      <a
        :class="{ active: sortField === undefined }"
        @click="changeSort(undefined)"
        href="javascript:;"
        >默认排序</a
      >
      <a
        :class="{ active: sortField === 'publishTime' }"
        @click="changeSort('publishTime')"
        href="javascript:;"
        >最新商品</a
      >
      <a
        :class="{ active: sortField === 'orderNum' }"
        @click="changeSort('orderNum')"
        href="javascript:;"
        >最高人气</a
      >
      <a
        :class="{ active: sortField === 'evaluateNum' }"
        @click="changeSort('evaluateNum')"
        href="javascript:;"
        >评论最多</a
      >
      <a href="javascript:;" @click="togglePriceSort">
        价格排序
        <i
          class="arrow up"
          :class="{ active: sortMethod === 'asc' && sortField === 'price' }"
        />
        <i
          class="arrow down"
          :class="{ active: sortMethod === 'desc' && sortField === 'price' }"
        />
      </a>
    </div>
    <div class="check">
      <XtxCheckbox>仅显示有货商品</XtxCheckbox>
      <XtxCheckbox>仅显示特惠商品</XtxCheckbox>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
export default {
  name: 'SubSort',
  setup (props, { emit }) {
    // 排序的条件
    const sortField = ref(undefined)
    // 升降序
    const sortMethod = ref('desc')

    // 点击价格 切换排序的逻辑
    const togglePriceSort = () => {
      // 切换排序条件为 价格
      sortField.value = 'price'
      // 升降序 取反
      sortMethod.value = sortMethod.value === 'desc' ? 'asc' : 'desc'
      emit('sortChange', {
        sortField: sortField.value,
        sortMethod: sortMethod.value
      })
    }

    // 修改排序条件
    const changeSort = newSort => {
      if (sortField.value !== newSort) {
        sortField.value = newSort
        // 通知父组件
        emit('sortChange', {
          sortField: newSort,
          sortMethod: sortMethod.value
        })
      }
    }

    return {
      sortField,
      togglePriceSort,
      sortMethod,
      changeSort
    }
  }
}
</script>
<style scoped lang="less">
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  margin-top: 25px;
  padding: 0 25px;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
